package ModelLayer;
import java.util.ArrayList;

/**
 * The address book is a container for the Class Person, it has an ArrayList and a private constructor
 * so that we can use a single instance of it.
 * 
 * @author Gruppe A 
 * @version 1.0
 */
public class AddressBook
{
    // instance variables - replace the example below with your own
    private ArrayList<Person>myPersons;
    private static AddressBook instance;

    /**
     * Constructor for objects of class AddressBook
     */
    private AddressBook()
    {
        // initialise instance variables
        myPersons = new ArrayList<Person>();
    }

    /**
     * Method for checking if an instance of the class allready exists, if it doesn't one is created.
     * @return AddressBook instance.
     */
    public static AddressBook getInstance()
    {
        if(instance == null)
        {
            instance = new AddressBook();
        }
        return instance;
    }

    /**
     * A mutator method for adding objects of the type Person to the ArrayList.
     * @param Person person.
     */
    public void addPerson(Person person)
    {
        myPersons.add(person);
    }

    /**
     * An accessor method for finding specific objects in the ArrayList, it takes a long variable as a parameter. And 
     * returns a specific object of Person
     * @param long id.
     * @return Person specific person.
     */
    public Person findPerson(long id)
    {
        int index = 0;
        boolean searching = true;
        while(index < myPersons.size() && searching)
        {
            Person filename = myPersons.get(index);
            if(filename.getId() == id)
            {
                searching = false;         
            }
            else
            {
                index++;
            }
        }
        if(!searching)
        {
            return myPersons.get(index);
        }
        else
        {
            return null;
        }
    }

    /**
     * A mutator method for reomving an object of Person from the ArrayList.
     * @param Person person.
     */
    public void removePerson(Person person)
    {
        myPersons.remove(person);
    }

    /**
     * A mutator method for updating the attributes of a specific Person object. Takes different parameters and substitutes
     * them for the original data. Thus updating the object.
     * @param long id
     * @param String name
     * @param String address
     * @param int postnr
     * @String city
     * @String phoneNumber
     */
    public void updatePerson(long id, String name, String address, int postnr, String city, String phoneNumber)
    {
        Person p = findPerson(id);
        if(id != 0)
        {

            if(name != null)
            {
                p.setName(name);
            }

            if(address != null)
            {
                p.setAddress(address);
            }

            if(postnr != 0)
            {
                p.setPostnr(postnr);
            }

            if(city != null)
            {
                p.setCity(city);
            }

            if(phoneNumber != null)
            {
                p.setPhoneNumber(phoneNumber);
            }

        }

    }

    /**
     * An accessor method for listing all the people in the addressbook.
     * @return ArrayList myPersons
     */
    public ArrayList listPersons()
    {
        ArrayList<String> persons = new ArrayList<String>();
        for(Person p: myPersons)
        {
            persons.add(" ID: "+Long.toString(p.getId()));
            persons.add(" Name: "+p.getName());
            persons.add(" Address: "+p.getAddress());
            persons.add(" Postal Number: "+Integer.toString(p.getPostnr()));
            persons.add(" City: "+p.getCity());
            persons.add(" Phone Number: "+p.getPhoneNumber());
            persons.add("\n");
            
        }
        return persons;
    }
}